package com.mashibing.jvm;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class ZoneTest {
    //    US/Central
    private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a z";
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);

    public static void main(String[] args)
    {
    /**
     * UTC时间与CST时间换算 UTC表示世界协调时间(Coordinated Universal Time)(正在使用)
     *
     * <p>CST表示中国标准时间(China Standard Time)(正在使用)
     *
     * <p>相关时区转换(时区缩写相同)
     *
     * <p>UTC (UTC) ↔ CST (Central Standard Time)
     *
     * <p>UTC (UTC) ↔ CST (Cuba Standard Time)
     */
    ZoneId fromTimeZone = ZoneId.of("Asia/Kolkata"); // Source timezone
        //ZoneId toTimeZone = ZoneId.of("America/New_York");  //Target timezone
        ZoneId toTimeZone = ZoneId.of("US/Central");  //CST  Central Standard Time
        //ZoneId toTimeZone = ZoneId.of("XXX");  //CST  Central Standard Time

        LocalDateTime today = LocalDateTime.now();          //Current time

        //Zoned date time at source timezone
        // ZonedDateTime currentISTime = today.atZone(fromTimeZone);
        ZonedDateTime currentISTime = today.atZone(ZoneId.systemDefault());


        //Zoned date time at target timezone
        ZonedDateTime currentETime = currentISTime.withZoneSameInstant(toTimeZone);

        //Format date time - optional
        System.out.println(formatter.format(currentISTime));
        System.out.println(formatter.format(currentETime));

        System.out.println(getSpecialZoneDateTime(today, "US/Central").toString());

        //ZoneId.of("US/Central");
    }

    public static LocalDateTime getSpecialZoneDateTime(LocalDateTime localDateTime, String zoneId) {
        long epochMilli = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        Instant instant = Instant.ofEpochMilli(epochMilli);
        ZoneId zone = ZoneId.of(zoneId);
        return LocalDateTime.ofInstant(instant, zone);
    }
}
